﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for.
    /// Project name: SMS_xoso
    /// channel: n/a
    /// command code: HN X Y
    /// created: 01/09/09 .
    /// Writer : Nampt + Tuanbc
    /// Company: VIT TELECOM
    /// </summary>

    public class A
    {
        #region Declare
        private SMS_MO WMO;
        private SMS_MT WMT;
        //private SMS_LIST WML;
        ArrayList arr = new ArrayList();
        //DateTime datetime;
        public A(SMS_MO MO)
        {
            WMO = MO;
        }

        public A()
        {
        }
        #endregion
        #region Method
        public void run()
        {
            XOSO_WAIT xs_wait = new XOSO_WAIT();
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            if (msg.Length > 0)//soi cau
            {
                string msgcontent = "";
                if (WMO.ServiceID.Replace("04", "") == "6772" || WMO.UserID == "84902222776" || (WMO.UserID == "84978925568"))
                {

                    msgcontent = GetReturnMsg(msg[0]);
                }
                else
                {
                    msgcontent = "De nhan CAU SO CHINH XAC NHAT soan tin: A <MaTinh> gui 6772.Vdu muon nhan CAU SO Thu Do soan tin: A TD gui 6772";
                }
                WMT.AddMessage(WMO.UserID, msgcontent, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();
            }
            else//nhan ket qua moi nhat
            {
                if (WMO.ServiceID.Replace("04", "") == "6272")//nhan ngay ket qua moi nhat
                {
                    if ((DateTime.Now.Hour >= 19) && (CheckCurrentResult() == false))//Neu nhan sau 7h ma chua co ket moi nhat thi o che do cho
                    {
                        string msgCHO = "6x72 xac nhan thong tin ban chuyen toi tong dai.Chung toi se gui toi ban ket qua XSTD ngay khi qua trinh quay so hoan tat";
                        WMT.AddMessage(WMO.UserID, msgCHO, SMS_ContentType.SMS_TEXT);
                        WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                        WMT.Insert();
                        xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 0, 1);
                    }
                    else
                    {
                        string strReturnMsg = GetReturnMsg();
                        if (strReturnMsg != "")
                        {
                            WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
                            WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                            WMT.Insert();
                        }
                    }
                }
                else if (WMO.ServiceID.Replace("04", "") == "6372")//nhan ket qua cho 3 ngay
                {
                    string msgCHO = "6x72 xac nhan thong tin ban chuyen toi tong dai.Chung toi se gui toi ban ket qua XSTD trong 02 ngay lien tiep.";
                    WMT.AddMessage(WMO.UserID, msgCHO, SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    WMT.Insert();
                    xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 0, 2);
                }
                else if (WMO.ServiceID.Replace("04", "") == "6472")//nhan ket qua cho 3 ngay
                {
                    string msgCHO = "6x72 xac nhan thong tin ban chuyen toi tong dai.Chung toi se gui toi ban ket qua XSTD trong 03 ngay lien tiep.";
                    WMT.AddMessage(WMO.UserID, msgCHO, SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    WMT.Insert();
                    xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 0, 3);
                }
                else if (WMO.ServiceID.Replace("04", "") == "6572")//Nhan ket qua truc tiep
                {
                    string msgCHO = "6x72 xac nhan thong tin ban chuyen toi tong dai.Chung toi se gui toi ban ket qua tuong thuat truc tiep tu truong quay.";
                    WMT.AddMessage(WMO.UserID, msgCHO, SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    WMT.Insert();
                    xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 1, 4);
                }
                else if (WMO.ServiceID.Replace("04", "") == "6672")//nhan ket qua cho 5 ngay
                {
                    string msgCHO = "6x72 xac nhan thong tin ban chuyen toi tong dai.Chung toi se gui toi ban ket qua XSTD trong 05 ngay lien tiep.";
                    WMT.AddMessage(WMO.UserID, msgCHO, SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    WMT.Insert();
                    xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 0, 5);
                }
                else if (WMO.ServiceID.Replace("04", "") == "6772")//nhan ket qua cho 10 ngay
                {
                    string msgCHO = "6x72 xac nhan thong tin ban chuyen toi tong dai.Chung toi se gui toi ban ket qua XSTD trong 10 ngay lien tiep.";
                    WMT.AddMessage(WMO.UserID, msgCHO, SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    WMT.Insert();
                    xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 0, 10);
                }
            }

        }
       
        private string GetReturnMsg()
        {
            string msgBody = String.Empty;
            string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
            string strSQl = "SELECT TOP 1 * FROM XOSO WHERE Mamien='MB' ORDER BY Ngay DESC";
            SqlConnection oledbConn = new SqlConnection(connString);
            SqlDataAdapter oleda = new SqlDataAdapter(strSQl, oledbConn);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            oledbConn.Open();// Open connection
            oleda.Fill(ds, "VIT");
            oledbConn.Close();
            dt = ds.Tables["VIT"];
            DateTime dateTime = DateTime.Parse(dt.Rows[0]["Ngay"].ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                msgBody = string.Concat("TD:", string.Concat(dateTime.Day, "/", dateTime.Month), "\n",
                    "DB:", dt.Rows[0]["GiaiDacBiet"].ToString().Trim().Replace(" ", ""), "\n",
                    "1:", dt.Rows[0]["GiaiNhat"].ToString().Trim().Replace(" ", ""), "\n",
                    "2:", dt.Rows[0]["GiaiNhi"].ToString().Trim().Replace(" ", ""), "\n",
                    "3:", dt.Rows[0]["GiaiBa"].ToString().Trim().Replace(" ", ""), "\n",
                    "4:", dt.Rows[0]["GiaiTu"].ToString().Trim().Replace(" ", ""), "\n",
                    "5:", dt.Rows[0]["GiaiNam"].ToString().Trim().Replace(" ", ""), "\n",
                    "6:", dt.Rows[0]["GiaiSau"].ToString().Trim().Replace(" ", ""), "\n",
                    "7:", dt.Rows[0]["GiaiBay"].ToString().Trim().Replace(" ", ""));
            }
            else
            {
                msgBody = ConfigSettings.ReadSetting("SMS_HELP_XS");
            }
            ds.Dispose();
            oledbConn.Dispose();
            return msgBody;
        }
        private string GetReturnMsg(string matinh)
        {
            string msgBody = String.Empty;
            if (matinh == "MB" || matinh == "HN") matinh = "TD";
            string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
            string strSQl = "SELECT TOP 1 * FROM A_SOICAU WHERE MaTinh='" + matinh + "'";
            SqlConnection oledbConn = new SqlConnection(connString);
            SqlDataAdapter oleda = new SqlDataAdapter(strSQl, oledbConn);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            oledbConn.Open();// Open connection
            oleda.Fill(ds, "VIT");
            oledbConn.Close();
            dt = ds.Tables["VIT"];
            if (ds.Tables[0].Rows.Count > 0)
            {
                DateTime dateTime = DateTime.Parse(dt.Rows[0]["NgayNhap"].ToString());
                msgBody = "CAU " + matinh + "[" + dateTime.ToString("dd.MM") + "]: " + dt.Rows[0]["NoiDung"].ToString().Trim();
            }
            ds.Dispose();
            oledbConn.Dispose();
            return msgBody;
        }
        //Neu co ket qua moi va da quay xong
        private Boolean CheckCurrentResult()
        {
            Boolean val = false;
            string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
            string strSQl = "SELECT GIAIDACBIET FROM XOSO WHERE Mamien='MB' and convert(nvarchar(10),Ngay,103)=convert(nvarchar(10),getdate(),103)";
            SqlConnection oledbConn = new SqlConnection(connString);
            SqlDataAdapter oleda = new SqlDataAdapter(strSQl, oledbConn);
            DataSet ds = new DataSet();
            oledbConn.Open();// Open connection
            oleda.Fill(ds, "VIT");
            oledbConn.Close();
            if ((ds.Tables["VIT"].Rows.Count > 0) && (!string.IsNullOrEmpty(ds.Tables["VIT"].Rows[0]["GIAIDACBIET"].ToString().Trim())))
                val = true;
            ds.Dispose();
            oledbConn.Dispose();
            return val;
        }
        #endregion
    }
}
